package mkis.service.root.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mkis.service.root.common.PageInfo;
import mkis.service.root.common.SearchInfo;
import mkis.service.root.data.model.Bbs;
import mkis.service.root.data.model.Member;
import mkis.service.root.service.BbsSevice;
import mkis.service.root.utils.MkisUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class BbsCTR {
	
	@Resource(name="bbsService")
	private BbsSevice bbsService;
	
	@Resource(name="mkisUtils")
	private MkisUtils mkisUtils;
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());
			
	@RequestMapping(value="/bbs/notice_list.do")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,PageInfo pageInfo,SearchInfo searchInfo,String boardCode) throws Exception{
		ModelAndView mav=new ModelAndView("bbs/notice_list");
		
		Member member = MkisUtils.getMember(request);
		
		String page=pageInfo.getPage();
		
		if(page==null || "".equals(page)){
			page="1";
		}
		int rowcnt=pageInfo.getRowcnt();
		int startRow=(Integer.parseInt(page)-1)*rowcnt;
		String input=searchInfo.getInput();
		String typeSelect=searchInfo.getTypeSelect();
				
		
		Map<String,Object> paramMap=mkisUtils.makeListParam(page, startRow, rowcnt, pageInfo, searchInfo);
		paramMap.put("boardCode", boardCode);
		
		Map<String,Object> resultMap=bbsService.getNoticeListAndCnt(paramMap);
		logger.debug("{}",resultMap);
		List<Bbs> list=(List<Bbs>) resultMap.get("list");
		int cnt=(Integer) resultMap.get("cnt");		
		
		mav.addObject("list", list);
		mav.addObject("page", Integer.parseInt(page));
		mav.addObject("totalcnt", cnt);
		
		
		mav.addObject("input", input);
		mav.addObject("typeSelect", typeSelect);
		mav.addObject("boardCode", boardCode);
		
		return mav;
	}
	
	@RequestMapping(value="/bbs/notice_view.do")
	public ModelAndView notice_view(HttpServletRequest request,HttpServletResponse response,String boardSeq) throws Exception {
		ModelAndView mav=new ModelAndView("bbs/notice_view");
		Bbs bbs=bbsService.getNotice(boardSeq);
		mav.addObject("bbs", bbs);
		return mav;
	}
	
	@RequestMapping(value="/bbs/notice_delete.do")
	public ModelAndView  notice_delete(HttpServletRequest request,HttpServletResponse response,String boardSeq) throws Exception {
		ModelAndView mav=new ModelAndView("bbs/notice_delete_complete");
		List<String> boardSeqList=new ArrayList<String>();
		
		boardSeqList.add(boardSeq);
		int cnt=bbsService.deleteNotice(boardSeqList);
		
		mav.addObject("cnt", cnt);
		
		return mav;
	}
	
	@RequestMapping(value="/bbs/notice_deleteList.do")
	public @ResponseBody String notice_deleteList(HttpServletRequest request,HttpServletResponse response,String boardSeq) throws Exception {
		String code=null;

		String[] arr_board_seq=boardSeq.split(",");
		
		List<String> boardSeqList=new ArrayList<String>();
		
		for(String board_seq:arr_board_seq){
			boardSeqList.add(board_seq);
		}
		
		int cnt=bbsService.deleteNotice(boardSeqList);
		
		if(cnt>0) {
			code="0000";
		} else {
			code="0001";
		}
		
		return code;
	}
}
